package com.pushmessgae.server;

import java.util.Hashtable;

import com.pushmessgae.server.util.SocketUtil;

public class ClientContainer implements ErrorInputListener{

	private final Hashtable<String, ClientThread> clientThreads = new Hashtable<String, ClientThread>();// 客户端
	
	private Object object = new Object();
	
	private ClientContainerListener containerListener;
	
	public void addClient(ClientThread clientThread){
		synchronized (object) {
			final String key = SocketUtil.getSocketAddress(clientThread.getSocket());
			clientThreads.put(key, clientThread);
			containerListener.addClient(clientThread);
			clientThread.setErrorInputListener(this);
		}
	}
	
	public void removeClient(ClientThread clientThread){
		synchronized (object) {
			final String key = SocketUtil.getSocketAddress(clientThread.getSocket());
			clientThreads.remove(key);
			containerListener.removeClient(clientThread);
		}
	}
	
	public int getSize(){
		synchronized (object) {
			return clientThreads.size();
		}
	}
	
	public void setContainerListener(ClientContainerListener containerListener) {
		this.containerListener = containerListener;
	}



	public interface ClientContainerListener {
		
		public void addClient(ClientThread clientThread);
		
		public void removeClient(ClientThread clientThread);
	}



	@Override
	public void errorInput(ClientThread clientThread) {
		this.removeClient(clientThread);
	}
	
}
